#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    int binary_search(vector<int>& nums, int target){
        int left = 0,right = nums.size()-1;
        while(left <= right){
            int mid = left + (right - left) / 2; // 防溢出
            if(target < nums[mid])
                right = mid-1;
            else if(target > nums[mid])
                left = mid + 1;
            else
                return mid; //返回下标
        }
        return -1;//表示没有找到
    }
    vector<int> find_range(vector<int>& nums,int index){
        int n = nums[index];
        int left=0,right=0;
        for(int i=index;i>=0;i--)
            if(nums[i]==n)  //向左遍历
                left=i;
        
        for(int i=index;i<nums.size();i++)
            if(nums[i]==n)  //向右遍历
                right=i;
        return {left,right};
    }
    vector<int> searchRange(vector<int>& nums, int target) {
        int index = binary_search(nums,target);
        if(index<0)
            return {-1,-1};
        return find_range(nums, index);
    }
};